package edu.pku.sei.modeler.editpolicies;

import org.eclipse.gef.Request;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.commands.UnexecutableCommand;
import org.eclipse.gef.editpolicies.ConnectionEditPolicy;
import org.eclipse.gef.requests.ChangeBoundsRequest;
import org.eclipse.gef.requests.GroupRequest;

import edu.pku.sei.modeler.actions.DeleteFromModelAction;
import edu.pku.sei.modeler.editparts.UMLLinkEditpart;
import edu.pku.sei.modeler.model.structure.MetaLink;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;
import edu.pku.sei.modeler.uml.actions.DeleteFromDiagramAction;
import edu.pku.sei.modeler.uml.commands.UMLConnectionDeleteCommand;
import edu.pku.sei.modeler.uml.commands.UMLConnectionDeleteCommandFactory;

public class UMLConnectionEditPolicy extends ConnectionEditPolicy {

	/**
	 * 
	 */
	public UMLConnectionEditPolicy() {
		super();
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	protected MetaLink getGraphEdge() {
		return (MetaLink)getHost().getModel();
	}
	
	/**
	 * 
	 * 
	 * @return 
	 */
	protected UMLLinkEditpart getGraphEdgeEditPart() {
		return (UMLLinkEditpart)getHost();
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editpolicies.ConnectionEditPolicy#getCommand(org.eclipse.gef.Request)
	 */
	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	public Command getCommand(Request request) {
		if(DeleteFromDiagramAction.DELETE_FROM_DIAGRAM_ACTION_REQUEST.equals(request.getType())) {
			return getDeleteFromDiagramCommand();
		}
		if(DeleteFromModelAction.DELETE_FROM_MODEL_ACTION_REQUEST.equals(request.getType())) {
			return getDeleteFromModelCommand();
		}
		
		if (REQ_DELETE.equals(request.getType())) {
			return getDeleteCommand((GroupRequest) request);
		} else if (request instanceof ChangeBoundsRequest
				&& REQ_ADD.equals(request.getType())) {
			ChangeBoundsRequest r = (ChangeBoundsRequest) request;
			r.getMoveDelta().x = 0;
			// return new
		}
		return null;
		
		//return super.getCommand(request);
	}

    /**
     * Return the appropriate delete from model command
     * @return
     */
    protected Command getDeleteFromModelCommand() {
        if(getGraphEdgeEditPart().getModel()!= null) {
        	return getDeleteCommandFactory().createCommand((MetaLink) getGraphEdgeEditPart().getModel(), false);
        } else {
        	return UnexecutableCommand.INSTANCE;
        }
    }

    /**
     * Return the delete factory used to return a delete command.
     * This method can be subclassed in order to return a specific factory.
     * @return
     */
    protected UMLConnectionDeleteCommandFactory getDeleteCommandFactory() {
        return UMLConnectionDeleteCommandFactory.eINSTANCE;
    }
	
	/* (non-Javadoc)
	 * @see org.eclipse.gef.editpolicies.ConnectionEditPolicy#getDeleteCommand(org.eclipse.gef.requests.GroupRequest)
	 */
	/**
	 * 
	 * 
	 * @param request 
	 * 
	 * @return 
	 */
	@Override
	protected Command getDeleteCommand(GroupRequest request) {
		return new UMLConnectionDeleteCommand(getGraphEdge(), true);
	}

	/**
	 * 
	 * 
	 * @return 
	 */
	protected Command getDeleteFromDiagramCommand() {
		MetaLink child = getGraphEdge();
		
		Command command = null;
		if((child == null) ) {
			throw new IllegalArgumentException("child or parent is null ");
		}
		command = new UMLConnectionDeleteCommand(child, true);
		
		return command;
	}

}
